package com.edu.zut.cs.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

/**
 * swagger2 配置类
 */
@Configuration
@EnableSwagger2   //开启Swagger2
public class Swagger2Config {

    /**
     * 当有多个分组时，要有多个@Bean Docket
     *
     */
//
//    @Bean
//    public Docket docket1(){
//        return new Docket(SWAGGER_2).groupName("许文科1");
//    }

    @Bean
    public Docket docket(Environment environment){

        //设置要显示的Swagger环境，在发布后不使用swagger，只在开发环境中使用swagger  配置文件
        Profiles profiles = Profiles.of("dev","test");
        //通过environment.acceptsProfiles判断是否处在自己设定的环境中
        boolean flag = environment.acceptsProfiles(profiles);


        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("许文科")
                //enable为是否启动Swagger，如果为false，则不能打开swagger页面
                .enable(flag)
                //select与build是配套的
                .select()
                //RequestHandlerSelectors 配置要扫描的接口位置
                // any()扫描全部，none()都不扫描
                //withClassAnnotation 扫描类上的注解
                .apis(RequestHandlerSelectors.basePackage("com.edu.zut.cs.controller"))
                //过滤什么路径
                .paths(PathSelectors.any())
                .build();
    }

    //配置 Swagger 信息
    private ApiInfo apiInfo() {
        Contact contact = new Contact("许文科","https://www.xuwenke.xyz","2036739377@qq.com");

         return new ApiInfo(
                 "Swagger文档",
                 "无",
                 "1.0",
                 "https://www.xuwenke.xyz",
                 contact,
                 "Apache 2.0",
                 "http://localhost:8081/doc.html",  //api文档路径
                 new ArrayList());

    }
}
